package com.laowang.serviceImpl;

import com.laowang.dto.OrderDTO;
import com.laowang.enums.ResultEnum;
import com.laowang.exception.SellException;
import com.laowang.sercice.BuyerService;
import com.laowang.sercice.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2019/1/16.
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService{
    @Autowired
    private OrderService orderService;
    @Override
    public OrderDTO findOrderOne(String openid, String orderId) {
        return checkOrderOpenid(openid,orderId);
    }

    @Override
    public OrderDTO cancel(String openid, String orderId) {
        OrderDTO orderDTO = checkOrderOpenid(openid, orderId);
        if(orderDTO==null){
            log.error("[取消订单] 查不到该订单 orderId={}",orderId);
            throw  new SellException(ResultEnum.CANCELORDER_NOT_EXIT);
        }
        return orderService.cancel(orderDTO);
    }
    private OrderDTO checkOrderOpenid(String openid, String orderId){
        OrderDTO orderDTO = orderService.findById(orderId);
        if(orderDTO==null){
            return null;
        }
        if(!orderDTO.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("[查询订单] 订单的openid不一致. openid={},orderDTO={}",openid,orderDTO);
            throw new SellException(ResultEnum.ORDER_OWEN_NOTSAME);
        }
        return orderDTO;
    }
}
